﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using shapes = MassIntellect.Drawing.Shapes;
using pres = MassIntellect.Presentation;

namespace MassIntellect.WorkIt.Config
{
    public partial class Form1 : Form
    {
        private BufferedGraphicsContext BGC = null;
        private BufferedGraphics grOffside = null;        

        public Form1()
        {
            InitializeComponent();
			
        }

    	private pres.View view = new pres.View();

    	private void Form1_Load(object sender, EventArgs e)
        {
        	pbBox.AllowDrop = true;
            BGC = BufferedGraphicsManager.Current;
			view.Window = new Rectangle(0, 0, pbBox.Width - 1, pbBox.Height - 1);
            grOffside = BGC.Allocate(pbBox.CreateGraphics(), view.Window);
            grOffside.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grOffside.Graphics.SmoothingMode = SmoothingMode.HighQuality;    		
            timer.Enabled = true;            
        }

        private void pbBox_Paint(object sender, PaintEventArgs e)
        {
            DrawPic();
        }

        private void DrawPic()
        {
            if (grOffside != null)
            {
                grOffside.Graphics.Clear(Color.White);
                var gr = grOffside.Graphics;
                gr.DrawRectangle(new Pen(Color.Blue) { DashPattern = new[] { 1f, 4f } }, 0, 0, pbBox.Width - 2, pbBox.Height - 2);
                var pen = new Pen(Color.LightGray) { DashPattern = new[] { 1f, 4f } };
                const int step = 16;
                for (var x = step; x < pbBox.Width; x += step)
                    gr.DrawLine(pen, x, 1, x, pbBox.Height);
                for (var y = step; y < pbBox.Height; y += step)
                    gr.DrawLine(pen, 1, y, pbBox.Width, y);
                view.DrawToGraphics(grOffside.Graphics);
                grOffside.Render();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            DrawPic();
        }

        private void pbBox_Resize(object sender, EventArgs e)
        {
			//if (grOffside != null)
			//    grOffside.Dispose();
            if (BGC != null)
            {
            	view.Width = pbBox.Width - 1;
            	view.Height = pbBox.Height - 1;
				grOffside = BGC.Allocate(pbBox.CreateGraphics(), new Rectangle(0, 0, view.Width, view.Height));
                grOffside.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grOffside.Graphics.SmoothingMode = SmoothingMode.HighQuality;                
                DrawPic();
            }
        }

		#region Mouse Events
		private void pbBox_MouseMove(object sender, MouseEventArgs e)
		{
			view.MouseMove(sender, e);
		}

		private void pbBox_MouseClick(object sender, MouseEventArgs e)
		{
			view.MouseClick(sender, e);
		}

		private void pbBox_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			view.MouseDoubleClick(sender, e);
		}

		private void pbBox_MouseDown(object sender, MouseEventArgs e)
		{

		}

		private void pbBox_MouseHover(object sender, EventArgs e)
		{

		}

		private void pbBox_MouseLeave(object sender, EventArgs e)
		{

		}

		private void pbBox_MouseUp(object sender, MouseEventArgs e)
		{

		}

		private void pbBox_DragDrop(object sender, DragEventArgs e)
		{
			//canv.OnDragDrop(e);
		}

		private void pbBox_DragEnter(object sender, DragEventArgs e)
		{
			//canv.OnDragEnter(e);
		}

		private void pbBox_DragLeave(object sender, EventArgs e)
		{
			//canv.OnDragLeave();
			DrawPic();
		}

		private void pbBox_DragOver(object sender, DragEventArgs e)
		{
			//canv.OnDragOver(e);
			DrawPic();
		}

		private void pbBox_GiveFeedback(object sender, GiveFeedbackEventArgs e)
		{
			//canv.OnGiveFeedback(e);
			DrawPic();
		}

		private void pbBox_MouseCaptureChanged(object sender, EventArgs e)
		{
			//canv.OnMouseCaptureChanged();
			DrawPic();
		}

		private void pbBox_MouseWheel(object sender, MouseEventArgs e)
		{
			//canv.OnMouseWheel(e);            
			DrawPic();
		} 
		#endregion
    }
}
